package com.zxy.demo.guava;

import java.util.Collections;
import java.util.List;

import org.junit.Test;

import com.google.common.base.MoreObjects;
import com.google.common.base.Optional;

/**
 * OptionalDemo.
 * 
 * Created by zxybird on 17/7/2.
 */
public class OptionalDemo {

  @Test
  public void test() throws Exception {
    // Optional<String> optional = Optional.fromNullable(null);
    // System.out.println(optional.or("空"));
    // optional = Optional.fromNullable("zxy");
    // System.out.println(optional.or("空"));

    Optional.absent();

    Optional<List<String>> list = Optional.fromNullable(getList());
    Optional.of(null).or(2);
    // List<String> list = getList();

    System.out.println(list.or(Collections.emptyList()).size());

  }

  @Test
  public void test2() throws Exception {

    List<String> list1 = Optional.fromNullable(getList()).or(Collections.emptyList());
    System.out.println(list1.size());

    List<String> tmp = getList();
    List<String> list2 = (null == tmp) ? Collections.emptyList() : tmp;

    List<String> list3 = getList();
    if (null == list3) {
      list3 = Collections.emptyList();
    }
  }

  /**
   *
   */
  @Test
  public void test3() throws Exception {
    List<String> list1 = MoreObjects.firstNonNull(getList(), Collections.emptyList());
  }

  List<String> getList() {
    return null;
  }

  List<String> getList(Integer len) {
    return null;
  }

}
